<?php
require_once 'Encode.php';
require_once 'noticeModel.php';
require_once 'DBConnector.php';
require_once 'view.php';
session_start();

$_func=e($_REQUEST['request']);
if ($_func==NULL){
	showNotice();
}else{
	$_func();
}

function selectNotice($noticeIndex=NULL){
	$noticeModel = new NoticeModel();
	$db = DBConnector::getDB();
	if($noticeIndex==NULL){
		$sth = $db->prepare("select * from tNotice order by NCreatedDate desc");
	} else {
		$sth = $db->prepare("select * from tPicture where tNotice={$noticeIndex}");
	}
	return $noticeModel->select($sth);
}

function showNotice(){
	$result=selectNotice();
	$view=new View($result);
	$view->render('notice.php');
}

function getJsonNoticeData(){
	$result=selectNotice();
	$jsonData=json_encode($result);
	print_r($jsonData);
}

function deleteNotice(){
	if ($_SESSION['token']==e($_REQUEST['token'])) {
		$_noticeIndex = e($_REQUEST['NIndex']);
		$noticeModel = new NoticeModel();
		$noticeModel->delete($_noticeIndex);
		showNotice();
	}	
}

function insertNotice(){
	$nTitle=e($_REQUEST['NTitle']);
	$nContent=e($_REQUEST['NContent']);
	$endDate = mktime(0, 0, 0, date("m")  , date("d")+7, date("Y"));
	$startDate = date("Y-m-d H:i:s");
	$noticeModel=new NoticeModel();
	$noticeModel->insert($nTitle,$nContent,$endDate,$startDate);
	showNotice();
}